﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using Telerik.Web.UI;

namespace AllNations
{
    public partial class ANMCMaster : MakeItWebMasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            var announcement = GetActiveAnnouncement();
            if (!string.IsNullOrEmpty(announcement))
                lblAnnouncement.Text = announcement;
            lblAnnouncement.Visible = !string.IsNullOrEmpty(announcement);

            litGoogleAnalytics.Visible = !(Page is MakeItWebAdminPage);
            var selectedItem = menuTopMenu.Items.Where(i => i.NavigateUrl == Request.AppRelativeCurrentExecutionFilePath || i.Value == SelectedSection);
            var item = selectedItem.FirstOrDefault();
            if (item != null)
                item.Selected = true;

            //// panelPageHeaderImage could be overwritten by the page.
            //if (panelPageHeaderImage != null)
            //{
            //    panelPageHeaderImage.Visible = CurrentHtmlPage.IncludeHeaderImageInd;
            //    if (panelPageHeaderImage.Visible && !string.IsNullOrEmpty(CurrentHtmlPage.HeaderImageUrl))
            //    {
            //        imgPageHeader.ImageUrl = CurrentHtmlPage.HeaderImageUrl;
            //    }
            //}
            //btnPageAdmin.Visible = IsPageAdministrator() && !Request.RawUrl.ToLower().Contains("/webappadmin/");
            //if (btnPageAdmin.Visible)
            //    btnPageAdmin.OnClientClick = string.Format("location='/webappadmin/editpage.aspx?HtmlPageId={0}'; return;", CurrentHtmlPage.HtmlPageId);
            AdministratorMenu.Visible = IsPageAdministrator() && (AdminMenuItems.Count() > 0); // && !Request.RawUrl.ToLower().Contains("/webappadmin/");
            if (AdministratorMenu.Visible)
            {
                AdministratorMenu.Items[0].Items.Clear();
                Array.ForEach(AdminMenuItems.ToArray(), i =>
                {
                    AdministratorMenu.Items[0].Items.Add(new Telerik.Web.UI.RadMenuItem() { Text = i.Text, NavigateUrl = i.NavigationUrl, ImageUrl = i.IconImageUrl });
                });
            }
        }

        private string GetActiveAnnouncement()
        {
            using (var ds = GenericQry.GetDataSet("proc_cm_GetActiveQuickAnnouncement", new StoredProcArgs()))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds.Tables[0].Rows[0]["Announcement"].ToString();
            }

            return string.Empty;
        }
    }
}